package com.xauto.pioneer.appuser.activity.mine.user;

import android.view.View;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.ModifyMobileDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

/***
 * 修改手机号
 * @author Lemon Thu 9 Mar 14:50
 */
public class EditMobileActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.tvEditMobile) TextView tvEditMobile;

    /** 需要修改的手机号*/
    private String editMobile;
    private boolean isEditSuccess;

    @Override
    protected int getContentView() {
        return R.layout.ac_edit_mobile;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.login_input_mobile);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        String mobile = getIntent().getStringExtra(Constants.EDIT_TYPE_MOBILE);
        tvEditMobile.setText("您当前的手机号为"+StringUtils.hideMobile(mobile));

        lHandler = new BaseLHttpHandler(mContext, true, true) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                isEditSuccess = true;
                tvEditMobile.setText("您当前的手机号为"+StringUtils.hideMobile(editMobile));
            }
        };
    }

    @OnClick(R.id.tvEditMobileSubmit)
    public void onClick() {
        ModifyMobileDialog modifyMobileDialog = new ModifyMobileDialog(mContext);
        modifyMobileDialog.show();
    }

    /**
     * 修改手机号
     * @param mobile
     * @param code
     */
    public void editMobile(String mobile, String code) {
        editMobile = mobile;
        loadEditMember(code);
    }

    void loadEditMember(String code) {
        new LHttpLib().editMemberInfo(mContext, memberId, Constants.EDIT_TYPE_MOBILE, editMobile, code, lHandler);
    }

    @Override
    public void onBackPressed() {
        if(isEditSuccess) {
            setResult(Constants.RESULT_EDIT_SUCCESS);
        }
        finish();
    }
}
